﻿using System;
using System.CodeDom;
using System.Data;
using System.Web.UI;
using System.ComponentModel;
using System.Web.Compilation;
//using EasyLib.AAA;
//using EasyLib.DB;
//using EasyLib.DB.AbstractLayer;
//using EasyLib.DB.Interfaces;
//using EasyLib.Utils;
//using NopSolutions.NopCommerce.Common.Utils;
using System.Collections;
using System.Web;
using System.Web.Caching;
using Tour.BLL;
using Tour.DAL;
using System.Collections.Generic;


[ExpressionPrefix("VSTExpression")]
[ExpressionEditor("VSTExpressionEditor")]

public class VSTExpressionBuilder : ExpressionBuilder
{
    // Create a method that will return the result 
    // set for the expression argument.
    static string keyResource = "Resource";
    public object GetEvalData(string resourceName, Type target, string entry)
    {
        if (HttpContext.Current.Cache[keyResource] == null)
        {
            CacheLocaleResourceString();
        }
        Hashtable ht = (Hashtable)HttpContext.Current.Cache[keyResource];
        string key = CurrentLanguage + "_" + resourceName;
        if (ht[key] != null)
            return ht[key].ToString();
        return "";
    }
    void CacheLocaleResourceString()
    {
        Hashtable ht = GetAllLocaleResourceString();
        HttpContext.Current.Cache.Insert(keyResource, ht, null, DateTime.Now.AddMinutes(10), TimeSpan.Zero, CacheItemPriority.Default, new CacheItemRemovedCallback(CachedItemRemoveCallBack));
    }
    void CachedItemRemoveCallBack(string key, object value, CacheItemRemovedReason reason)
    {
        if (HttpContext.Current.Cache[keyResource] == null)
        {
            CacheLocaleResourceString();
        }
    }
    Hashtable GetAllLocaleResourceString()
    {
        Hashtable resource = new Hashtable();
        List<LocaleStringResourceEntity> ls = LocaleStringResourceManager.CreateInstant().SelectAll();
        for (int i = 0; i < ls.Count; i++)
        {
            string key = ls[i].LanguageID + "_" + ls[i].ResourceName;
            string value = ls[i].ResourceValue;
            resource.Add(key, value);
        }
        return resource;
    }
    public override object EvaluateExpression(object target, BoundPropertyEntry entry,
    object parsedData, ExpressionBuilderContext context)
    {
        return GetEvalData(entry.Expression, target.GetType(), entry.Name);
    }

    public override CodeExpression GetCodeExpression(BoundPropertyEntry entry,
    object parsedData, ExpressionBuilderContext context)
    {
        Type type1 = entry.DeclaringType;
        PropertyDescriptor descriptor1 = TypeDescriptor.GetProperties(type1)[entry.PropertyInfo.Name];
        CodeExpression[] expressionArray1 = new CodeExpression[3];
        expressionArray1[0] = new CodePrimitiveExpression(entry.Expression.Trim());
        expressionArray1[1] = new CodeTypeOfExpression(type1);
        expressionArray1[2] = new CodePrimitiveExpression(entry.Name);
        return new CodeCastExpression(descriptor1.PropertyType, new CodeMethodInvokeExpression(new
        CodeTypeReferenceExpression(base.GetType()), "GetEvalData", expressionArray1));
    }

    public override bool SupportsEvaluate
    {
        get { return true; }
    }

    public static int CurrentLanguage
    {
        get
        {
            if (CommonHelper.GetCookieInt("VST.Language") > 0)
                return CommonHelper.GetCookieInt("VST.Language");
            else
                return 1;
        }
        set
        {
            if (value == null)
                return;
        }
    }
}

